
package dao.impl;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Calendar;
import dao.AppointmentDAO;
import model.Appointment;

public class AppointmentDAOImpl implements AppointmentDAO{

	private HashMap<String, Appointment> appointments;
    private int 	SeqId = 0;
    private Integer	PatientId = 1;
    private Integer ProviderId = 1;
    private int 	version = 1;
    private Long   date;
   
    
  	public AppointmentDAOImpl() {
    	if (appointments.size() == 0) {
			add("pat1_prov1", PatientId, ProviderId, date, 1, "hour long appt at this point in time");
			add("pat2_prov1", ++PatientId, ProviderId, date, 1, "hour long appt at this point in time");
			add("pat3_prov1",++PatientId, ProviderId, date, 1, "hour long appt at this point in time");
	
		}
   	    System.out.println("Creating Appointment DAO Implementation");
    }
    // create
	public boolean add( String description, Integer patientId, Integer providerId,
			Long startDateTime, int duration, String comment) {
		Appointment appt = new Appointment(null, version, description, patientId, providerId, 
				startDateTime, duration, comment);
		return add(appt);

	}
	public boolean add(Appointment appointment) {
	  	appointment.setId(SeqId);
		appointments.put(appointment.getDescription(), appointment);
        SeqId++;
		return true;
	}
	
    public Appointment[] getAll() {
    	Collection<Appointment> collection = appointments.values();
		return collection.toArray(new Appointment[collection.size()]);
	}	
       
  	public long getCount() {
		return appointments.size();
	}
	
  	public Appointment getById(Integer id) {
		return appointments.get(id);
	}

	public Appointment[] getByDescription(String name) {
		List<Appointment> found = new ArrayList<Appointment>();
		for (Appointment p : appointments.values()) {
			if (p.getDescription().equals(name)) {
				found.add(p);
			}
		}
		return found.toArray(new Appointment[found.size()]);
	}
	public Appointment[] getByPatientId(Integer Id) {
		List<Appointment> found = new ArrayList<Appointment>();
		for (Appointment p : appointments.values()) {
			if (p.getPatientId().equals(Id)) {
				found.add(p);
			}
		}
		return found.toArray(new Appointment[found.size()]);
	}
	public Appointment[] getByProviderId(Integer Id) {
		List<Appointment> found = new ArrayList<Appointment>();
		for (Appointment p : appointments.values()) {
			if (p.getProviderId().equals(Id)) {
				found.add(p);
			}
		}
		return found.toArray(new Appointment[found.size()]);
	}
	public Appointment[] getByPatientProviderId(Integer PatientId, Integer ProviderId) {
		List<Appointment> found = new ArrayList<Appointment>();
		for (Appointment p : appointments.values()) {
			if (p.getProviderId().equals(ProviderId) && p.getPatientId().equals(PatientId)) {
				found.add(p);
			}
		}
		return found.toArray(new Appointment[found.size()]);
	}
	
	public boolean update(Appointment appointment) {
    	appointments.put(appointment.getDescription(), appointment);
		return false;
	}
	
	public boolean delete(Appointment appointment) {
		appointments.remove(appointment.getDescription());
	    return true;
	}
	
    public Appointment save(Appointment appointment) {
    	appointment.setId(SeqId);
    	appointments.put(appointment.getDescription(), appointment);
        SeqId++;
		return appointment;
	}
  
}
